package api

import (
	"api/config"
	"api/internal/api/request"
	"api/internal/api/response"
	"api/internal/handler"
	"flag"
	"github.com/gin-gonic/gin"
	pb "shop_srv/proto"
	"strconv"
)

var (
	shopAddress = config.SrvConfigs.ShopSrv.Host + ":" + strconv.Itoa(config.SrvConfigs.ShopSrv.Port)
	shopServer  = flag.String(config.SrvConfigs.ShopSrv.Name, shopAddress, "The server address")
	shopSrv     = handler.NewClient().ShopServer(shopServer)
)

func Create(c *gin.Context) {
	var json *request.Create
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	create, err := shopSrv.Create(c, &pb.CreateRequest{
		ShopName:       json.ShopName,
		ShopAddress:    json.ShopAddress,
		ShopImage:      json.ShopImage,
		ShopType:       int64(json.ShopType),
		ShopMessage:    json.ShopMessage,
		ShopMain:       json.ShopMain,
		PeopleBuyPrice: float32(json.PeopleBuyPrice),
		StartingBuy:    float32(json.StartingBuy),
		ShopPaper:      json.ShopPaper,
		ShopMath:       json.ShopMath,
		ShopClassId:    int64(json.ShopClassId),
	})
	if err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	response.Response(c, 200, "添加成功", create.Id)
}

func List(c *gin.Context) {
	var json *request.List
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	list, err := shopSrv.List(c, &pb.ListRequest{
		Page:     int64(json.Page),
		PageSize: int64(json.PageSize),
		KeyWord:  json.KeyWord,
	})
	if err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	response.Response(c, 200, "展示成功", list)
}
